from typing_extensions import Annotated
from langchain_core.tools import tool
from tools import get_llm
from langchain_core.prompts import ChatPromptTemplate


@tool
def weather(city: Annotated[str, '城市名称']):
    '''
    查询城市天气
    :param city:
    :return:
    '''
    return '天气真好'


@tool
def get_city_info(city: Annotated[str, '城市名称']):
    '''
    查询城市相关信息
    :param city:
    :return:
    '''
    template = ChatPromptTemplate(
        [
            ('human', '{city}天气怎么样'),
            ('system', '你是一名优秀的导游，根据用户信息回答城市相关特色')
        ]
    )
    chain = template | get_llm()
    return chain.invoke({'city': city}).content


llm = get_llm()
llm_with_tool = llm.bind_tools([weather, get_city_info])
result = llm_with_tool.invoke('我想去成都玩 有什么推荐的地方吗')
for item in result.tool_calls:
    print(item)
    func_name = item['name']
    args = item['args']
    func = eval(func_name)
    tool_result = func.invoke(args)
    print(tool_result)
